package com.hsylient.javase.chapter01;

import org.junit.Test;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class MyByte {

    @Test
    public void testField() {
        System.out.println("byte类型占空间大小(字节)：" + Byte.BYTES);
        System.out.println("byte类型占空间大小(bit)：" + Byte.SIZE);
        System.out.println("byte类型的最大值：" + Byte.MAX_VALUE);
        System.out.println("byte类型的最小值：" + Byte.MIN_VALUE);
    }
    public static void main(String[] args) {

//
//        byte a = 12;
//        System.out.println(a);
//
//        a += 1;
//        System.out.println(a + 2);
//
//        System.out.println(-(-128) + 127 + 1);

        byte[] bb = {40};
        String s = new String(bb, StandardCharsets.UTF_8);
        System.out.println(s);

        String content = "����������法师法师是";
        byte[] gbks = content.getBytes(StandardCharsets.UTF_8);
        System.out.println(Arrays.toString(gbks));
        System.out.println(new String(gbks, Charset.forName("GBK")));

    }

    @Test
    public void testMessyCode() {
        String content = "翰翰翰翰翰翰翰翰翰翰翰翰";
        // 使用GBK编码中文
        byte[] contentGBK = content.getBytes(Charset.forName("GBK"));
        System.out.println(Arrays.toString(contentGBK));
        // 使用UTF-8解码GBK编码
        String contentUTF8 = new String(contentGBK, StandardCharsets.UTF_8);
        // 首次输出乱码
        System.out.println(contentUTF8);
        // 将乱码结果再编码
        byte[] secondGBK = contentUTF8.getBytes(StandardCharsets.UTF_8);
        System.out.println(Arrays.toString(secondGBK));
        // 再解码
        String target = new String(secondGBK, Charset.forName("GBK"));
        System.out.println(target);
    }
}
